package ms.platform.system.controller;

import ms.platform.system.model.Admin;
import ms.platform.system.model.Manufacturer;
import ms.platform.system.service.ManufacturerService;
import ms.platform.util.PageUtils;
import ms.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/manufacturerManage/")
public class ManufacturerController extends BaseController {

    @Autowired
    private ManufacturerService manufacturerService;

    /**
     * 所有组织
     *
     * @return
     */
    @RequestMapping(value = "/manufacturer/list")
    public String getAllManufacturer(@RequestParam Map<String,Object> map, HttpServletRequest request) {
        try {
            if (!StringUtil.isNullOrBlock(map.get("orgCode"))) {
                if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode((String) map.get("orgCode")))) {
                    String fac = (String) map.get("orgCode");
                    map.put("orgCode",new String(fac.getBytes("ISO-8859-1"), "utf-8"));
                }
            }
            if (!StringUtil.isNullOrBlock(map.get("orgName"))) {
                if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode((String) map.get("orgName")))) {
                    String mode = (String) map.get("orgName");
                    map.put("orgName",new String(mode.getBytes("ISO-8859-1"), "utf-8"));
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        map.put("count", manufacturerService.queryManufacturerCount(map));
        List<Manufacturer> manufacturerAllList = manufacturerService.getAllManufacturer(PageUtils.Page(request,map));
        request.setAttribute("manufacturerList", manufacturerAllList);
        PageUtils.calculate(map);
        request.setAttribute("searchHistory", map);
        request.getSession().setAttribute("getAllManufacturer", map);
        return "system/manufacturerManage/manufacturerIndex";
    }

    @RequestMapping(value = "/updateOrAdd/manufacturer")
    @ResponseBody
    public Object insertManufacturer(Manufacturer manufacturer, HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("adminUser");//当前操作的人
        if (manufacturer.getId() != null) {
            manufacturer.setParentId(1000);
            manufacturer.setUpdatedBy(admin.getUsername());
            manufacturerService.updateManufacturer(manufacturer);
        } else {
            manufacturer.setStatus("1");
            manufacturer.setOrgType("F");
            manufacturer.setParentId(1000);
            manufacturer.setInsertedBy(admin.getUsername());
            manufacturerService.insertManufacturer(manufacturer);
        }
        return returnSuccess("操作成功", manufacturer);
    }


    //删除
    @RequestMapping(value = "/delete/manufacturer", method = RequestMethod.GET)
    public ModelAndView deleteManufacturer(Integer id, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        manufacturerService.deleteManufacturer(id);
        modelAndView.setViewName("redirect:/system/manufacturerManage/manufacturer/list");
        return modelAndView;
    }

    //搜索
    @RequestMapping(value = "/search/manufacturer")
    @ResponseBody
    public ModelAndView searchManufacturer(Manufacturer manufacturer, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        List<Manufacturer> manufacturers = manufacturerService.searchManufacturer(manufacturer);
        modelAndView.addObject("searchHistory", manufacturer);
        modelAndView.addObject("manufacturerList", manufacturers);
        modelAndView.setViewName("system/manufacturerManage/manufacturerIndex");
        return modelAndView;
    }

    @RequestMapping(value = "/manufacturerById")
    public String getManufacturer(Integer id, HttpServletRequest request) {
        if (id != null) {
            Manufacturer manufacturer = manufacturerService.getManufacturer(id);
            request.setAttribute("manufacturer", manufacturer);
        }
        return "system/manufacturerManage/editManufacturer";
    }
}
